package com.cjm.algorithm.leetcode.hash;

import java.util.ArrayDeque;
import java.util.Deque;

public class LengthLongestPath388 {
    private final char enter = '\n';
    private final char tab = '\t';
    public int lengthLongestPath(String input) {
        int n = input.length();
        int pos = 0;
        int ans = 0;
        Deque<Integer> stack = new ArrayDeque<>();

        while (pos < n) {
            int depth = 1;
            while (pos < n && input.charAt(pos) == tab) {
                pos++;
                depth++;
            }
            boolean isFile = false;
            int len = 0;
            while (pos < n && input.charAt(pos) != enter) {
                if (input.charAt(pos) == '.') {
                    isFile = true;
                }
                len++;
                pos++;
            }
            pos++;
            while (stack.size() >= depth) {
                stack.pop();
            }
            if (!stack.isEmpty()) {
                len += stack.peek() + 1;
            }
            if (isFile) {
                ans = Math.max(ans, len);
            } else {
                stack.push(len);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        String xx = "dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext";
        LengthLongestPath388 x = new LengthLongestPath388();
        int i = x.lengthLongestPath(xx);
        System.err.println(i);
    }
}
